package com.gzhryc.shared_device.oem.micro.queue.handlers.profit;

import com.gzhryc.common.event.AbsDelayHandler;
import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.shared_device.bxscn.code.dao.db.BXSCNOrder;
import com.gzhryc.shared_device.bxscn.code.services.BXSCNOrderService;
import com.gzhryc.shared_device.bxscn.code.dao.db.BXSCNShopOrder;
import com.gzhryc.shared_device.bxscn.code.services.BXSCNShopOrderService;
import com.gzhryc.shared_device.cdb.code.dao.db.CDBOrder;
import com.gzhryc.shared_device.cdb.code.services.CDBOrderService;
import com.gzhryc.shared_device.cdcz.code.dao.db.CDCZOrder;
import com.gzhryc.shared_device.cdcz.code.services.CDCZOrderService;
import com.gzhryc.shared_device.oem.code.business.EBusinessType;
import com.gzhryc.shared_device.oem.code.member.dao.db.MemberBusinessOrder;
import com.gzhryc.shared_device.oem.code.member.dao.enums.MemberBusinessOrderEnum;
import com.gzhryc.shared_device.oem.code.member.services.MemberBusinessOrderService;
import com.gzhryc.shared_device.oem.code.place.dao.db.PlacePhysicalStore;
import com.gzhryc.shared_device.oem.code.place.services.PlacePhysicalStoreService;
import com.gzhryc.shared_device.oem.code.profit.dao.db.BusinessOrder;
import com.gzhryc.shared_device.oem.code.profit.dao.db.BusinessOrderUserProfit;
import com.gzhryc.shared_device.oem.code.profit.event.UserProfitDivideEvent;
import com.gzhryc.shared_device.oem.code.profit.service.BusinessOrderService;
import com.gzhryc.shared_device.oem.code.profit.service.BusinessOrderUserProfitService;
import com.gzhryc.shared_device.oem.code.statistics.services.ProjectProfitDayStatisticsService;
import com.gzhryc.shared_device.oem.code.statistics.services.UserProfitDayStatisticsService;
import com.gzhryc.shared_device.oem.code.user.ERoleType;
import com.gzhryc.shared_device.pcl.code.dao.db.PCLOrder;
import com.gzhryc.shared_device.pcl.code.services.PCLOrderService;

import java.util.List;

public class UserProfitDivideEventHandler extends AbsDelayHandler<UserProfitDivideEvent> {

    static Logger log = Logger.getLogger(UserProfitDivideEventHandler.class);

    @Override
    public boolean execute(UserProfitDivideEvent event) {
        try{
            BusinessOrder businessOrder = null;
            try {
                MemberBusinessOrder memberBusinessOrder = MemberBusinessOrderService.self(event.getCreateDate()).getByTradeNo(event.getTradeNo());
                if (memberBusinessOrder != null && MemberBusinessOrderEnum.State.Finish.index().equals(memberBusinessOrder.getState())) {
                    PlacePhysicalStore physicalStore = null;
                    if (EBusinessType.CDCZ.index().equals(memberBusinessOrder.getBusinessType())) {
                        CDCZOrder cdczOrder = CDCZOrderService.self(event.getCreateDate()).getByTradeNo(event.getTradeNo());
                        physicalStore = PlacePhysicalStoreService.self().getById(cdczOrder.getPlaceId());
                        businessOrder = BusinessOrderService.self(cdczOrder.getCreateDate()).create(cdczOrder, physicalStore);
                    } else if (EBusinessType.CDB.index().equals(memberBusinessOrder.getBusinessType())) {
                        CDBOrder cdbOrder = CDBOrderService.self(event.getCreateDate()).getByTradeNo(event.getTradeNo());
                        physicalStore = PlacePhysicalStoreService.self().getById(cdbOrder.getLeasePlaceId());
                        businessOrder = BusinessOrderService.self(cdbOrder.getCreateDate()).create(cdbOrder, physicalStore);
                    } else if (EBusinessType.PCL.index().equals(memberBusinessOrder.getBusinessType())) {
                        PCLOrder pclOrder = PCLOrderService.self(event.getCreateDate()).getByTradeNo(event.getTradeNo());
                        physicalStore = PlacePhysicalStoreService.self().getById(pclOrder.getPlaceId());
                        businessOrder = BusinessOrderService.self(pclOrder.getCreateDate()).create(pclOrder, physicalStore);
                    } else if (EBusinessType.BXSCN.index().equals(memberBusinessOrder.getBusinessType())) {
                        BXSCNOrder bxscnOrder = BXSCNOrderService.self(event.getCreateDate()).getByTradeNo(event.getTradeNo());
                        physicalStore = PlacePhysicalStoreService.self().getById(bxscnOrder.getLeasePlaceId());
                        businessOrder = BusinessOrderService.self(bxscnOrder.getCreateDate()).create(bxscnOrder, physicalStore);
                    } else if (EBusinessType.BXSCN_SHOP.index().equals(memberBusinessOrder.getBusinessType())) {
                        BXSCNShopOrder bxscnShopOrder = BXSCNShopOrderService.self(event.getCreateDate()).getByTradeNo(event.getTradeNo());
                        physicalStore = PlacePhysicalStoreService.self().getById(bxscnShopOrder.getPlaceId());
                        businessOrder = BusinessOrderService.self(bxscnShopOrder.getCreateDate()).create(bxscnShopOrder, physicalStore);
                    } else {
                        log.error("暂不支持的设备功能分成");
                        return true;
                    }
                } else {
                    log.error("未找到会员订单信息或会员订单状态不正确，事件：{{0:json}}", event);
                    return true;
                }
            } catch (LogicException e) {
                log.error(e.getMessage(), e);
            }

            if(businessOrder != null) {
                log.info("分成订单：{{0:json}}",businessOrder);
                //获取旧的撤销分成
                List<BusinessOrderUserProfit> oldUserProfitList = BusinessOrderUserProfitService.self(businessOrder.getCreateDate()).findByTradeNo(businessOrder.getTradeNo());
                if (BusinessOrderUserProfitService.self(event.getCreateDate()).dividedIncome(businessOrder, event.getOperateInfo())) {
                    //获取最新数据
                    businessOrder = BusinessOrderService.self(event.getCreateDate()).getByTradeNo(event.getTradeNo());
                    //统计处理
                    List<BusinessOrderUserProfit> userProfitList = BusinessOrderUserProfitService.self(businessOrder.getCreateDate()).findDivideByTradeNo(businessOrder.getTradeNo());
                    if(userProfitList != null && userProfitList.size() > 0){
                        for(BusinessOrderUserProfit userProfit : userProfitList){
                            Long oldIncomeMoney = null;
                            if(oldUserProfitList != null && oldUserProfitList.size() > 0){
                                for(BusinessOrderUserProfit oldUserProfit : oldUserProfitList){
                                    if(oldUserProfit.getUserId().equals(userProfit.getUserId())){
                                        oldIncomeMoney = oldUserProfit.getMoney();
                                        break;
                                    }
                                }
                            }
                            if(ERoleType.Sys.index().equals(userProfit.getRoleType())){
                                ProjectProfitDayStatisticsService.self().addOneIncomeData(businessOrder.getDivideDate(),
                                        businessOrder.getIncomeMoney(),userProfit.getMoney(),oldIncomeMoney,businessOrder.getCancelDate());
                            }else{
                                UserProfitDayStatisticsService.self().addOneIncomeData(userProfit.getUserId(),userProfit.getName(),userProfit.getPhone()
                                        ,businessOrder.getDivideDate(),businessOrder.getIncomeMoney(),userProfit.getMoney(),oldIncomeMoney,businessOrder.getCancelDate());
                            }
                        }
                    }
                }else{
                    log.error("分成失败，订单信息：{{0:json}}",businessOrder);
                }
            }else{
                log.error("未找到分成订单信息，事件：{{0:json}}",event);
            }
        }catch (Exception e){
            log.error(e.getMessage(),e);
        }
        return true;
    }
}
